/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package canteen.sys.view.individual;

import canteen.sys.SessionFactoryClass;
import canteen.sys.model.entity.BillDetail;
import canteen.sys.model.entity.BillMaster;
import canteen.sys.model.entity.DepartmentMaster;
import canteen.sys.model.entity.ItemMaster;
import canteen.sys.model.entity.ItemTypeMaster;
import canteen.sys.model.entity.LoginMaster;
import canteen.sys.model.generic.HibernateDAOFactory;
import canteen.sys.view.ActionView;
import canteen.sys.view.ComboboxRecInfo;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author TNT
 */
public class BillEntryTnT extends javax.swing.JInternalFrame implements ActionView<ItemMaster> {

    HibernateDAOFactory hibernateDAOFactory = HibernateDAOFactory.getInstance();
    DefaultTableModel tbItemsModel;
    DefaultTableModel tbItemPickModel;
    List<ItemMaster> listItem;
    List<ItemMaster> listItemPick;
    LoginMaster lm;

    /**
     * Creates new form BillEntryTnT
     */
    public BillEntryTnT() {
        initComponents();
        tbItemPickModel = (DefaultTableModel) tableItemPicked.getModel();
        tbItemsModel = (DefaultTableModel) tbItems.getModel();
        listItemPick = new ArrayList<>();
        clearTableItem();
        clearTableItemPick();
        java.util.Date date = new java.util.Date();
        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        txtDate.setText(dateFormat.format(date));
        calCoupenDate.setDate(date);
        lm = SessionFactoryClass.getInstance().getUser();
        if (lm != null) {
            showListItemType();
            showListDepartment();
            listSelectionListener();
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        editorLayer = new javax.swing.JLayeredPane();
        jLabel1 = new javax.swing.JLabel();
        txtNo = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtCoupenNo = new javax.swing.JTextField();
        calCoupenDate = new org.freixas.jcalendar.JCalendarCombo();
        jLabel3 = new javax.swing.JLabel();
        txtCouponRate = new javax.swing.JTextField();
        txtDate = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        cbDepartment = new javax.swing.JComboBox();
        itemDetailLayer = new javax.swing.JLayeredPane();
        txtItemQty = new javax.swing.JTextField();
        txtItemRate = new javax.swing.JTextField();
        btAdd = new javax.swing.JButton();
        btRemove = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableItemPicked = new javax.swing.JTable();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        cbItemType = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tbItems = new javax.swing.JTable();
        txtItemName = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        actionLayer = new javax.swing.JLayeredPane();
        btSave = new javax.swing.JButton();
        btCancel = new javax.swing.JButton();

        setClosable(true);
        setTitle("Create Bill");

        editorLayer.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        editorLayer.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N

        jLabel1.setText("No");
        jLabel1.setBounds(10, 20, 30, 14);
        editorLayer.add(jLabel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtNo.setEnabled(false);
        txtNo.setBounds(100, 20, 170, 20);
        editorLayer.add(txtNo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel5.setText("Date");
        jLabel5.setBounds(10, 50, 50, 14);
        editorLayer.add(jLabel5, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel6.setText("Coupen No");
        jLabel6.setBounds(10, 80, 90, 14);
        editorLayer.add(jLabel6, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel7.setText("Coupen Date");
        jLabel7.setBounds(10, 110, 90, 14);
        editorLayer.add(jLabel7, javax.swing.JLayeredPane.DEFAULT_LAYER);
        txtCoupenNo.setBounds(100, 80, 170, 20);
        editorLayer.add(txtCoupenNo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        calCoupenDate.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "MM/dd/yyyy" }));
        calCoupenDate.setDate(new java.util.Date(1348094944000L));
        calCoupenDate.setDateFormat(new SimpleDateFormat("MM/dd/yyyy"));
        calCoupenDate.setBounds(100, 110, 170, 20);
        editorLayer.add(calCoupenDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel3.setText("Coupen Rate");
        jLabel3.setBounds(10, 140, 80, 20);
        editorLayer.add(jLabel3, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtCouponRate.setEditable(false);
        txtCouponRate.setBounds(100, 140, 170, 20);
        editorLayer.add(txtCouponRate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtDate.setEditable(false);
        txtDate.setBounds(100, 50, 170, 20);
        editorLayer.add(txtDate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel8.setText("Department :");
        jLabel8.setBounds(10, 220, 90, 20);
        editorLayer.add(jLabel8, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbDepartment.setBounds(100, 220, 170, 20);
        editorLayer.add(cbDepartment, javax.swing.JLayeredPane.DEFAULT_LAYER);

        itemDetailLayer.setBorder(javax.swing.BorderFactory.createTitledBorder("Item Details"));
        txtItemQty.setBounds(230, 240, 40, 20);
        itemDetailLayer.add(txtItemQty, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtItemRate.setEditable(false);
        txtItemRate.setBounds(280, 240, 110, 20);
        itemDetailLayer.add(txtItemRate, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btAdd.setText("Add");
        btAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btAddActionPerformed(evt);
            }
        });
        btAdd.setBounds(400, 210, 80, 23);
        itemDetailLayer.add(btAdd, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btRemove.setText("Remove");
        btRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btRemoveActionPerformed(evt);
            }
        });
        btRemove.setBounds(400, 240, 80, 23);
        itemDetailLayer.add(btRemove, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tableItemPicked.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "No.", "ItemName", "Qty", "Rate", "Amount"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableItemPicked);

        jScrollPane1.setBounds(10, 270, 460, 190);
        itemDetailLayer.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel9.setText("Item Name");
        jLabel9.setBounds(80, 220, 60, 14);
        itemDetailLayer.add(jLabel9, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel10.setText("Item Rate");
        jLabel10.setBounds(300, 220, 60, 14);
        itemDetailLayer.add(jLabel10, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtTotal.setEditable(false);
        txtTotal.setFont(new java.awt.Font("Tempus Sans ITC", 1, 18)); // NOI18N
        txtTotal.setForeground(new java.awt.Color(255, 51, 0));
        txtTotal.setBounds(350, 470, 130, 30);
        itemDetailLayer.add(txtTotal, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel12.setText("Total");
        jLabel12.setBounds(290, 470, 50, 30);
        itemDetailLayer.add(jLabel12, javax.swing.JLayeredPane.DEFAULT_LAYER);

        cbItemType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbItemTypeItemStateChanged(evt);
            }
        });
        cbItemType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbItemTypeActionPerformed(evt);
            }
        });
        cbItemType.setBounds(70, 20, 170, 20);
        itemDetailLayer.add(cbItemType, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel2.setText("Item Type");
        jLabel2.setBounds(10, 20, 90, 20);
        itemDetailLayer.add(jLabel2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        tbItems.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(tbItems);

        jScrollPane2.setBounds(12, 50, 460, 140);
        itemDetailLayer.add(jScrollPane2, javax.swing.JLayeredPane.DEFAULT_LAYER);

        txtItemName.setEnabled(false);
        txtItemName.setBounds(79, 240, 140, 20);
        itemDetailLayer.add(txtItemName, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jLabel11.setText("Item Qty");
        jLabel11.setBounds(230, 220, 60, 14);
        itemDetailLayer.add(jLabel11, javax.swing.JLayeredPane.DEFAULT_LAYER);

        actionLayer.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        btSave.setText("Save");
        btSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btSaveActionPerformed(evt);
            }
        });
        btSave.setBounds(600, 10, 80, 23);
        actionLayer.add(btSave, javax.swing.JLayeredPane.DEFAULT_LAYER);

        btCancel.setText("Cancel");
        btCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btCancelActionPerformed(evt);
            }
        });
        btCancel.setBounds(680, 10, 80, 23);
        actionLayer.add(btCancel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(actionLayer)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(editorLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 278, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(itemDetailLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 483, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editorLayer, javax.swing.GroupLayout.DEFAULT_SIZE, 510, Short.MAX_VALUE)
                    .addComponent(itemDetailLayer))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(actionLayer, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btAddActionPerformed
        // TODO add your handling code here:
        try {
            if (tbItems.getSelectedRow() != -1) {
                if (tbItems.getRowCount() > 0) {
                    int indexItem = tbItems.getSelectedRow();
                    int qty = Integer.parseInt(txtItemQty.getText());
                    if (qty <= 0) {
                        JOptionPane.showMessageDialog(this, "Quantity must >0",
                                "Warning", JOptionPane.WARNING_MESSAGE);
                        return;
                    }
                    addRow(getObject(indexItem), true);
                    txtTotal.setText("$" + getTotalItemPicked());
                } else {
                    JOptionPane.showMessageDialog(this, "No item to add");
                }
            } else {
                JOptionPane.showMessageDialog(this, "You must select item to add");
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Must input correct format! Integer",
                    "Warning", JOptionPane.WARNING_MESSAGE);
        }
        //String name = listItem.get(1).getItemName();
    }//GEN-LAST:event_btAddActionPerformed

    private void btRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btRemoveActionPerformed
        // TODO add your handling code here:
        if (tableItemPicked.getSelectedRow() != -1) {
            int indexItemPick = tableItemPicked.getSelectedRow();
            deleteRow(indexItemPick);
            txtTotal.setText("$" + getTotalItemPicked());
        } else {
            JOptionPane.showMessageDialog(this, "You must select row to add");
        }
    }//GEN-LAST:event_btRemoveActionPerformed

    private void cbItemTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbItemTypeItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cbItemTypeItemStateChanged

    private void cbItemTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbItemTypeActionPerformed
        // TODO add your handling code here:
        if (cbItemType.getItemCount() > 0) {
            ComboboxRecInfo<ItemTypeMaster> combobox =
                    (ComboboxRecInfo<ItemTypeMaster>) cbItemType.getSelectedItem();
            List<ItemMaster> listItemToShow = hibernateDAOFactory.getItemMasterDAO()
                    .findAllByItemTypeIdAndStatus(combobox.getKeyData().getItemTypeId(), true);
            if (listItem != null) {
                listItem.clear();
                clearTableItem();
            }
            listItem = listItemToShow;
            showListItem(listItemToShow);
        }
    }//GEN-LAST:event_cbItemTypeActionPerformed

    private void btSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btSaveActionPerformed
        // TODO add your handling code here:
        if (listItemPick.size() > 0
                && cbDepartment.getItemCount() > 0) {
            BillMaster bm = new BillMaster();
            bm.setCoupenDate(new Date(calCoupenDate.getCalendar().getTimeInMillis()));
            bm.setCoupenNo(txtCoupenNo.getText());
            bm.setLoginMaster(lm);
            ComboboxRecInfo<DepartmentMaster> comboboxDepartment =
                    (ComboboxRecInfo<DepartmentMaster>) cbDepartment.getSelectedItem();
            bm.setDepartment(comboboxDepartment.getKeyData());
            bm.setStatus(0);
            Calendar calendar = Calendar.getInstance();
            bm.setbDate(new Date(calendar.getTimeInMillis()));
            BillMaster billMasterResult = hibernateDAOFactory.getBillMasterDAO().makePersistent(bm);
            txtNo.setText(billMasterResult.getbId() + "");
            if (billMasterResult != null) {
                for (ItemMaster itemMaster : listItemPick) {
                    BillDetail bd = new BillDetail();
                    bd.setBill(bm);
                    bd.setItem(itemMaster);
                    bd.setItemQty(itemMaster.getQuantity());
                    bd.setItemRate(itemMaster.getItemRate());
                    bd.setItemSrNo(Integer.parseInt(itemMaster.getItemId() + "" + billMasterResult.getbId()));
                    hibernateDAOFactory.getBillDetailDAO().makePersistent(bd);
                    ItemMaster im = hibernateDAOFactory.getItemMasterDAO().findById(itemMaster.getItemId());
                    itemMaster.setQuantity(im.getQuantity() - bd.getItemQty());
                    hibernateDAOFactory.getItemMasterDAO().makePersistent(itemMaster);
                }
            }
            clearTableItemPick();
            JOptionPane.showMessageDialog(this, "Place Order Success!");
        } else {
            JOptionPane.showMessageDialog(this, "No item to buy!", "Warning", JOptionPane.WARNING_MESSAGE);
        }
    }//GEN-LAST:event_btSaveActionPerformed

    private void btCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btCancelActionPerformed
        // TODO add your handling code here:
        clearTableItemPick();
    }//GEN-LAST:event_btCancelActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLayeredPane actionLayer;
    private javax.swing.JButton btAdd;
    private javax.swing.JButton btCancel;
    private javax.swing.JButton btRemove;
    private javax.swing.JButton btSave;
    private org.freixas.jcalendar.JCalendarCombo calCoupenDate;
    private javax.swing.JComboBox cbDepartment;
    private javax.swing.JComboBox cbItemType;
    private javax.swing.JLayeredPane editorLayer;
    private javax.swing.JLayeredPane itemDetailLayer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tableItemPicked;
    private javax.swing.JTable tbItems;
    private javax.swing.JTextField txtCoupenNo;
    private javax.swing.JTextField txtCouponRate;
    private javax.swing.JTextField txtDate;
    private javax.swing.JTextField txtItemName;
    private javax.swing.JTextField txtItemQty;
    private javax.swing.JTextField txtItemRate;
    private javax.swing.JTextField txtNo;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables

    //for tbItemPickModel
    @Override
    public void deleteRow(int index) {
        if (tbItemPickModel.getRowCount() > 0) {
            listItemPick.remove(index);
            tbItemPickModel.removeRow(index);
        } else {
            JOptionPane.showMessageDialog(this, "No item to delete!");
        }
    }

    @Override
    public void updateRow(ItemMaster row, int index) {
        try {
            if (tableItemPicked.getRowCount() > 0) {
                for (ItemMaster itemMaster : listItemPick) {
                    if (itemMaster.getItemId() == row.getItemId()) {
                        int indexListItemPick = listItemPick.indexOf(itemMaster);
                        int preQty = itemMaster.getQuantity();
                        if (preQty + Integer.parseInt(txtItemQty.getText()) > row.getQuantity()) {
                            JOptionPane.showMessageDialog(this, "Item in inventory is: "
                                    + row.getQuantity()
                                    + "\nMust input less than!");
                            break;
                        } else {
                            row.setQuantity(preQty + Integer.parseInt(txtItemQty.getText()));
                            itemMaster.setQuantity(row.getQuantity());
                            listItemPick.set(indexListItemPick, itemMaster);
                            tableItemPicked.setValueAt(row.getQuantity(), index, 2);
                            tableItemPicked.setValueAt("$" + row.getQuantity() * row.getItemRate(), index, 4);
                        }
                        break;
                    }
                }
            }
        } catch (NumberFormatException | NullPointerException ex) {
        }
    }

    @Override
    public void addRow(ItemMaster row, boolean posion) {
        int number = 0, count = 0;
        boolean check = false;
        try {
            if (tableItemPicked.getRowCount() > 0) {
                for (ItemMaster itemMaster : listItemPick) {
                    if (itemMaster.getItemId() == row.getItemId()) {
                        check = true;
                        break;
                    }
                    ++count;
                }
            }
            if (check == false) {

                int qty = Integer.parseInt(txtItemQty.getText());
                if (qty > row.getQuantity()) {
                    JOptionPane.showMessageDialog(this, "Quantity must < " + row.getQuantity(),
                            "Warning", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                if (qty <= 0) {
                    JOptionPane.showMessageDialog(this, "Quantity must >0",
                            "Warning", JOptionPane.WARNING_MESSAGE);
                    return;
                }
                row.setQuantity(qty);
                listItemPick.add(0, row);
                if (tableItemPicked.getRowCount() > 0) {
                    number = Integer.parseInt(tableItemPicked.getValueAt(0, 0).toString());
                }
                tbItemPickModel.insertRow(0,
                        new Object[]{number + 1, row.getItemName(), row.getQuantity(),
                            "$" + row.getItemRate(),
                            "$" + row.getItemRate() * row.getQuantity()});
            } else {
                updateRow(row, count);
            }


        } catch (NumberFormatException | NullPointerException ex) {
        }
    }

    @Override
    public void showList(List<ItemMaster> list) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void showDetail(ItemMaster object) {
        txtItemName.setText(object.getItemName());
        txtItemRate.setText("$" + object.getItemRate());
    }

    @Override
    public ItemMaster getObject(int index) {
        ItemMaster im = new ItemMaster();
        try {
            if (listItem != null && listItem.size() > 0) {
                im.setItemId(listItem.get(listItem.size() - 1 - index).getItemId());
                im.setItemName(listItem.get(listItem.size() - 1 - index).getItemName());
                im.setItemRate(listItem.get(listItem.size() - 1 - index).getItemRate());
                im.setItemType(listItem.get(listItem.size() - 1 - index).getItemType());
                im.setQuantity(listItem.get(listItem.size() - 1 - index).getQuantity());
                im.setStatus(listItem.get(listItem.size() - 1 - index).isStatus());
            }
        } catch (Exception ex) {
        }
        return im;
    }

    @Override
    public void setObject(ItemMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean validObject(ItemMaster object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    //for tbItemsModel

    private void showListItemType() {
        List<ItemTypeMaster> listItemType = hibernateDAOFactory
                .getItemTypeMasterDAO().findAllByStatus(true);
        for (ItemTypeMaster itemTypeMaster : listItemType) {
            ComboboxRecInfo<ItemTypeMaster> combobox = new ComboboxRecInfo<>();
            combobox.setName(itemTypeMaster.getItemTypeName());
            combobox.setKeyData(itemTypeMaster);
            cbItemType.addItem(combobox);
        }
    }

    private void showListItem(List<ItemMaster> listItemToShow) {
        try {
            if (listItemToShow != null && listItemToShow.size() > 0) {
                for (ItemMaster itemMaster : listItemToShow) {
                    tbItemsModel.insertRow(0, new Object[]{itemMaster.getItemName(),
                                itemMaster.getQuantity(),
                                "$" + itemMaster.getItemRate()});
                }
            }
        } catch (NullPointerException ex) {
        }
    }

    private void clearTableItem() {
        try {
            if (tbItems.getRowCount() > 0) {
                if (listItem != null) {
                    listItem.clear();
                }
                tbItemsModel.setDataVector(null,
                        new Object[]{"Item Name", "Quantity", "Rate"});

            }
        } catch (NullPointerException ex) {
        }
    }

    private void clearTableItemPick() {
        try {
            if (tableItemPicked.getRowCount() > 0) {
                if (listItemPick != null) {
                    listItemPick.clear();
                }
                tbItemPickModel.setDataVector(null,
                        new Object[]{"No", "Item Name", "Quantity", "Rate", "Amount"});
                txtTotal.setText("");
            }
        } catch (NullPointerException ex) {
        }
    }

    private double getTotalItemPicked() {
        double total = 0;
        if (listItemPick.size() > 0) {
            for (ItemMaster itemMaster : listItemPick) {
                total += itemMaster.getItemRate() * itemMaster.getQuantity();
            }
        }
        return total;
    }

    public final void listSelectionListener() {
        tbItems.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                int rowSelected = tbItems.getSelectedRow();
                showDetail(getObject(rowSelected));
            }
        });
    }

    private void showListDepartment() {
        List<DepartmentMaster> listDepartment = hibernateDAOFactory
                .getDepartmentMasterDAO().findAllByStatus(true);
        for (DepartmentMaster departmentMaster : listDepartment) {
            ComboboxRecInfo<DepartmentMaster> combobox = new ComboboxRecInfo<>();
            combobox.setName(departmentMaster.getDepartmentName());
            combobox.setKeyData(departmentMaster);
            cbDepartment.addItem(combobox);
        }
    }
}
